package com.fafa.tankgame05;

import java.util.Vector;

public class EnemyTank extends Tank implements Runnable {
    Vector<Shot> shots = new Vector<>();
    //boolean isLive = true;

    public EnemyTank(int x, int y) {
        super(x, y);
    }


    @Override
    public void run() {
        while (isLive) {
            //判断子弹数量
            if(shots.isEmpty())
            {

                //加入子弹
                Shot shot = null;
                switch (getDirect()) {
                    case 0:
                        shot = new Shot(getX() + 15, getY() - 10, 0);
                        break;
                    case 1:
                        shot = new Shot(getX() + 15, getY() + 60, 1);
                        break;
                    case 2:
                        shot = new Shot(getX() - 10, getY() + 15, 2);
                        break;
                    case 3:
                        shot = new Shot(getX() + 60, getY() + 15, 3);
                        break;
                    default:
                        shot = new Shot(getX() + 15, getY() - 10, 0);
                        break;

                }

                this.shots.add(shot);
                new Thread(shot).start();
            }

            //随机改变方向
            switch (getDirect()) {//上下左右
                case 0:
                    for (int i = 0; i < 30; i++) {
                        moveUp();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 1:
                    for (int i = 0; i < 30; i++) {
                        moveDown();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
                case 2:
                    for (int i = 0; i < 30; i++) {
                        moveLeft();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                    break;
                case 3:
                    for (int i = 0; i < 30; i++) {
                        moveRight();
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    break;
            }
            setDirect((int) (Math.random() * 4));;
        }


    }
}
